// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;


        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[] array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public int put(int key, int value) {
        int index = key % array.length;
        Node cur = array[index];
        //遍历当前列表，看是否存在当前值
        while (cur != null) {
            if (cur.key == key) {
                cur.value = value;
            }
            cur = cur.next;
        }
        //若无当前值，则进行头插法
        Node node = new Node(key, value);
        node.next = array[index];
        array[index] = node;
        size++;
        //判断是否超载
        if (loadFactor()>=LOAD_FACTOR){
            //扩容
            resize();
        }
        return 0;
    }


    private void resize() {
        Node[] newArr=new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            Node cur=array[i];
            while(cur!=null){
                //遍历链表，将数据储存到新数组
               int newIndex=cur.key% newArr.length;
               Node curN=cur.next;
               cur.next=newArr[newIndex];
               newArr[newIndex]=cur;
               cur=curN;
            }
        }
        array=newArr;
    }


    private double loadFactor() {
        return size * 1.0 / array.length;
    }


    public HashBucket() {
        array=new Node[10];
    }


    public int get(int key) {
         int index=key%array.length;
         Node cur=array[index];
         while(cur!=null){
             if (cur.key==key){
                 return cur.value;
             }
             cur=cur.next;
         }
         return -1;
    }
}